#include "socialwhale.h"
#include "logic/user.h"
#include "gui/startwindow.h"
#include "gui/loginwindow.h"
#include "gui/homewindow.h"

#include <QtPlugin>
#include <QApplication>
#include <QtDebug>
#include <QFile>
#include <QByteArray>
#include <QLayout>
#include <QDir>
#include <QString>

SocialWhale::SocialWhale() : QObject() {
}

void SocialWhale::init() {
    // these are used in QSettings so that we don't specify them every time
    QCoreApplication::setOrganizationName( "SocialWhale" );
    QCoreApplication::setOrganizationDomain( "socialwhale.com" );
    QCoreApplication::setApplicationName(  "SocialWhale" );

    QDir::setCurrent( this->whaleDirectory().absolutePath() );

    this->initTheme();
    this->initPlugins();

    // get active user instance
    User * user = User::instance();

    // initialize user (try to login etc)
    user->init();

    // choose starting window
    this->chooseWindow( user );

    // change window according to user actions
    connect( user, SIGNAL( loginStartRequest() ), this, SLOT( showLoginWindow() ) );
    connect( user, SIGNAL( loggedIn() ), this, SLOT( showHomeWindow() ) );
    connect( user, SIGNAL( loggedOut() ), this, SLOT( showStartWindow() ) );
}

void SocialWhale::initTheme() {
    QDir themesDir = this->whaleDirectory();
    themesDir.cd( "themes" );
    this->mThemeManager = new ThemeManager( themesDir, "brown" );
}

void SocialWhale::initPlugins() {
    QDir pluginsDir = this->whaleDirectory();
    pluginsDir.cd( "plugins" );
    this->mPluginHandler = new PluginHandler( pluginsDir );
    this->mPluginHandler->loadPlugins();
}

void SocialWhale::chooseWindow( User * user ) {
    if ( user->isLoggedIn() ) {
        this->showHomeWindow();
    }
    else {
        this->showStartWindow();
    }
}

// call this before calling show() on a new window
// if this is not called, there will be two windows open
void SocialWhale::hideActiveWindow() {
    if ( qApp->activeWindow() != 0 ) { // just for safety
        qApp->activeWindow()->close();
        qapp->activeWindow()->deleteLater();
    }
}

// logged out start screen
void SocialWhale::showStartWindow() {
    this->hideActiveWindow();

    StartWindow * win = new StartWindow();
    win->show();
}

// login form screen
void SocialWhale::showLoginWindow() {
    this->hideActiveWindow();

    LoginWindow * win = new LoginWindow();
    win->show();
}

// logged in dashboard
void SocialWhale::showHomeWindow() {
    this->hideActiveWindow();

    HomeWindow * win = new HomeWindow( this->mPluginHandler, this->mThemeManager );
    win->show();
}

QDir SocialWhale::whaleDirectory() {
    // get user home directory
    QDir swDir = QDir::home();
    
    #if defined(Q_OS_WIN)
        // CamelCase directory on Windows
        const QString dirName = "SocialWhale";
    #else
        // lowercase hidden directory on Linux/Mac
        const QString dirName = ".socialwhale";
    #endif

    // try changing to SocialWhale directory
    if ( !swDir.cd( dirName ) ) {
        // directory does not exist, set it up
        this->setUpWhaleDirectory( swDir, dirName );
    }

    return swDir;
}

/* For some reason SocialWhale folder does not exist on home directory.
 * It should have been created by the install script.
 *
 * Create a new one so that we can go on.
 */
void SocialWhale::setUpWhaleDirectory( QDir & dir, const QString dirName ) {
    if ( !dir.mkdir( dirName ) ) {
        // maybe display a friendlier message?
        qDebug() << "Could not create SocialWhale directory on" << dir.path();
        exit( 1 );
    }
    dir.cd( dirName );

    // ok directory created, create subdirectories too
    dir.mkdir( "plugins" );
    dir.mkdir( "themes" );

    // no plugins and themes, maybe add a few somehow?
}
